<template>
	<view class="container">
		<star-nav title="设置中心" :type="3" icon="/static/myimages/m11.png"></star-nav>
		<view class="contenr">
			<u-form labelWidth="220rpx" labelPosition="top" :model="userInfo" ref="uForm"
				:labelStyle="{ color: '#FFFFFF', fontSize: '28rpx',opacity:'0.6' }">
				<scroll-view :scroll-top="scrollTop" scroll-y="true" class="scroll-Y">
					<view class="mine">
						<view class="QQTitle">
							<image src="/static/myimages/m2.png" mode="widthFix"
								style="width: 50rpx;margin-right: 10rpx;">
							</image>
							<view>个人信息</view>
						</view>
						<view>
							<view class="userImg" @click="choShow=true">
								<image v-if="imgShow" @error="imgError" :src="uploadUrl+userInfo.avatarUrl" mode=""
									style="width: 90rpx;height:90rpx;border-radius: 90rpx;"></image>
								<image v-else src="/static/myimages/m6.png" mode=""
									style="width: 90rpx;border-radius: 90rpx;height:90rpx;"></image>
							</view>
							<view class="userTitle">点击更换头像</view>
						</view>
						<u-form-item label="昵称">
							<view class="ipt">
								<u-input maxlength="10" fontSize="32rpx" color="#FFFFFF" placeholder="请输入昵称"
									border="none" v-model="userInfo.nickname"></u-input>
							</view>
						</u-form-item>
					</view>
					<view class="admin">
						<view class="QQTitle">
							<image src="/static/myimages/m3.png" mode="widthFix"
								style="width: 50rpx;margin-right: 10rpx;">
							</image>
							<view>账号信息</view>
						</view>
						<view style="width: 100%;">
							<u-form-item label="手机号">
								<view class="ipt">
									<u-input disabledColor="#22304D" :disabled="true" maxlength="11" fontSize="32rpx"
										color="#FFFFFF" placeholder="请输入手机号" border="none"
										v-model="userInfo.mobile1"></u-input>
								</view>
							</u-form-item>
							<u-form-item label="交易密码">
								<view class="ipt">
									<u-input disabledColor="#22304D" maxlength="6" fontSize="32rpx" color="#FFFFFF"
										placeholder="请输入交易密码" border="none" v-model="userInfo.transPwd"
										type="number"></u-input>
								</view>
							</u-form-item>
							<!-- <u-form-item label="绑定支付宝">
								<u-button :custom-style="customStyleBtn3"
									@click="setCertification">{{userInfo.alstatus==0?'去绑定':'解除绑定'}}</u-button>
							</u-form-item> -->
						</view>
					</view>
					<view class="qq">
						<view class="QQTitle">
							<image src="/static/myimages/m1.png" mode="widthFix"
								style="width: 40rpx;margin-right: 10rpx;">
							</image>
							<view>社交信息</view>
						</view>
						<view style="width: 100%;">
							<u-form-item label="微信账号">
								<view class="ipt">
									<u-input placeholder="请输入微信账号" border="none" fontSize="32rpx" color="#FFFFFF"
										placeholderClass="placeholderClass" v-model="userInfo.wechatId"></u-input>
								</view>
							</u-form-item>
							<u-form-item label="支付宝账号">
								<view class="ipt">
									<u-input fontSize="32rpx" color="#FFFFFF" placeholderClass="placeholderClass"
										placeholder="请输入支付宝账号" border="none" v-model="userInfo.alNumber"></u-input>
								</view>
							</u-form-item>
							<u-form-item label="姓名">
								<view class="ipt">
									<u-input placeholder="请输入姓名" border="none" fontSize="32rpx" color="#FFFFFF"
										placeholderClass="placeholderClass" v-model="userInfo.name"></u-input>
								</view>
							</u-form-item>
							<u-form-item label="身份证号码">
								<view class="ipt">
									<u-input fontSize="32rpx" color="#FFFFFF" placeholderClass="placeholderClass"
										placeholder="请输入身份证号码" border="none" v-model="userInfo.idCard"></u-input>
								</view>
							</u-form-item>
						</view>

					</view>
					<view class="submit">
						<u-button :custom-style="customStyleBtn1" :loading="loading" @click="submit">
							保存设置
						</u-button>
						<u-button :custom-style="customStyleBtn2" @click="setLogout">
							退出登录
						</u-button>
					</view>

					<view style="height: 50rpx;"></view>

				</scroll-view>
			</u-form>
		</view>
		<choiceVue :show="choShow" @success="choShow=false" @setSuccess="setModel" />
	</view>
</template>

<script>
	import {
		uniGetToken
	} from '@/utils/common.js'
	import {
		myUserSee,
		myUserUpdate,
		alipayAccredit,
		cancAlipayAuth
	} from '@/api/my.js'
	import {
		logout
	} from '@/api/login.js'
	import config from '@/config.js'
	import choiceVue from './components/choice.vue';
	export default {
		components: {
			choiceVue
		},
		data() {
			return {
				uploadUrl: config.uploadUrl,
				choShow: false,
				userInfo: {
					alstatus: 0, //支付宝认证状态:0=未认证 1=已认证
					alNumber: '', //支付宝i
					avatarUrl: '', //头像
					mobile: 0, //手机号
					nickname: '', //昵称
					wechatId: '', //微信号
					name: '', //姓名
					idCard: '', //身份证号码
				},
				loading: false,
				customStyleBtn3: {
					width: '616rpx',
					height: '96rpx',
					borderRadius: '30rpx',
					background: "#357CF8",
					fontSize: '33rpx',
					fontWeight: 800,
					color: '#FFFFFF',
					border: 'none',
					lineHight: '50rpx !important',
					letterSpacing: '1px',
					isEye: false
				},
				customStyleBtn2: {
					width: '650rpx',
					height: '96rpx',
					borderRadius: '96rpx',
					background: "#18253A",
					fontSize: '33rpx',
					fontWeight: 800,
					color: '#F04544',
					border: 'none',
					lineHight: '50rpx !important',
					letterSpacing: '1px',
					isEye: false,
					border: '4rpx solid #F04544'
				},
				customStyleBtn1: {
					width: '650rpx',
					height: '96rpx',
					borderRadius: '96rpx',
					background: "#357CF8",
					fontSize: '33rpx',
					fontWeight: 800,
					color: '#FFFFFF',
					border: 'none',
					lineHight: '50rpx !important',
					letterSpacing: '1px',
					isEye: false
				},
				scrollTop: 0,
				type: 0, //0相册，1拍照
				imgShow: true
			}
		},
		onLoad() {
			this.getUser()
		},
		methods: {
			imgError() {
				this.imgShow = false
			},
			setSlice(str) {
				let mobile = str.slice(0, 3) + '****' + str.slice(-4)
				return mobile
			},
			setUpload() {
				uni.chooseImage({
					count: 1,
					sourceType: [this.type == 0 ? 'album' : 'camera'], //album 从相册选择 camera 使用相机
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						uni.uploadFile({
							url: this.uploadUrl + "/API/Settings/upload", //仅为示例，非真实的接口地址
							filePath: tempFilePaths[0],
							name: 'file',
							header: {
								'Authorization': uniGetToken(),
							},
							formData: {
								'user': 'test'
							},
							success: uploadFileRes => {
								let url = JSON.parse(uploadFileRes.data).data.url
								this.imgShow = true
								console.log('url:', url);
								this.userInfo.avatarUrl = url
							}
						});
					}
				});
			},

			// 保存设置
			async submit() {
				try {
					this.loading = true
					let res = await myUserUpdate(this.userInfo)
					uni.showToast({
						title: '保存成功',
						icon: 'none'
					})
					this.loading = false
				} catch (error) {
					this.loading = false
				}

			},
			// 退出登录
			setLogout() {
				uni.showModal({
					title: '提示',
					content: '确认退出登录？',
					success: async res => {
						if (res.confirm) {
							const token = uni.getStorageSync('accessToken');
							let data = {
								authorizationHeader: token
							}
							await logout(data)
							// 登录页
							uni.reLaunch({
								url: '/pages/login/index'
							})
							uni.clearStorageSync()

						}
					}
				});
			},
			async getUser() {
				let res = await myUserSee()
				this.userInfo = res.data
				// this.userInfo.mobile = this.setSlice(this.userInfo.mobile)
				this.userInfo.mobile1 = this.setSlice(this.userInfo.mobile)

			},
			setModel(type) {

				this.type = type
				this.choShow = false
				this.setUpload()
			},
			// 认证
			async setCertification() {
				if (this.userInfo.alstatus == 1) {
					uni.showModal({
						title: '提示',
						content: '确认解绑支付宝？',
						success: async res => {
							if (res.confirm) {
								await cancAlipayAuth()
								uni.showToast({
									title: '解绑成功',
									icon: 'none'
								})
								this.getUser()
							} else if (res.cancel) {}
						}
					});
				}
				// 等于0  去认证支付宝
				if (this.userInfo.alstatus == 0) {
					const systemInfo = uni.getSystemInfoSync();
					if (systemInfo.platform === 'android') {
						// #ifdef APP-PLUS  
						const AlipayAuth = uni.requireNativePlugin(
							"DHQ-AlipayAuth")
						AlipayAuth.login({
								appId: '2021005189690695', //你在支付宝平台申请的App ID
								scheme: 'AndroidSchemes', // 需要传到支付宝SDK的scheme,注意需要在manifest.json-->App其他常用配置-->UrlSchemes中配置Android和iOS的
								scope: 'auth_user', //默认为auth_user,需要实名认证的把auth_user替换
								init: 'init' //init字段名应该是state才对，忘记改回来了，不过不影响，这里init字段传入后对应是state的值，可根据支付宝的要求自己修改，默认则传入”init”
							},
							res => {
								if (res.authStatus == 'USER_CANCEL') {
									uni.showToast({
										title: '拒绝授权无法登录',
										icon: 'none'
									})
								}
								if (res.result_code == 'SUCCESS') {
									let data = {
										code: res.auth_code
									}
									alipayAccredit(data).then(res => {
										uni.showToast({
											title: '绑定成功',
											icon: 'none'
										})
										this.getUser()
									})
								}
							})
						// #endif
					} else if (systemInfo.platform === 'ios') {
						// #ifdef APP-PLUS
						const AlipayAuth = uni.requireNativePlugin(
							"DHQ-AlipayAuth")
						AlipayAuth.login({
								appId: '2021005189690695', //你在支付宝平台申请的App ID
								scheme: 'IOSSchemes', // 需要传到支付宝SDK的scheme,注意需要在manifest.json-->App其他常用配置-->UrlSchemes中配置Android和iOS的
								scope: 'auth_user', //默认为auth_user,需要实名认证的把auth_user替换
								init: 'init' //init字段名应该是state才对，忘记改回来了，不过不影响，这里init字段传入后对应是state的值，可根据支付宝的要求自己修改，默认则传入”init”
							},
							res => {
								if (res.authStatus == 'USER_CANCEL') {
									uni.showToast({
										title: '拒绝授权无法登录',
										icon: 'none'
									})
								}
								if (res.result_code == 'SUCCESS') {
									let data = {
										code: res.auth_code
									}
									alipayAccredit(data).then(res => {
										alipayAccredit(data).then(res => {
											uni.showToast({
												title: '绑定成功',
												icon: 'none'
											})
											this.getUser()
										})
									})
								}
							})

						// #endif
					}
				}

			}
		}
	}
</script>

<style scoped>
	.userTitle {
		margin-top: 15rpx;
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 28rpx;
		color: #FFFFFF;
		opacity: 0.5;
	}

	.userImg {
		width: 150rpx;
		height: 150rpx;
		border: 4rpx dashed #264374;
		border-radius: 150rpx;
		display: flex;
		align-items: center;
		justify-content: center;

	}

	.ipt {
		width: 616rpx;
		height: 100rpx;
		border-radius: 30rpx;
		background-color: #22304D;
		border: 4rpx solid #264374;
		display: flex;
		align-items: center;
		padding: 0 30rpx;
		box-sizing: border-box;
	}

	.QQTitle {
		width: 100%;
		height: 60rpx;
		display: flex;
		align-items: center;
		justify-content: flex-start;
		font-size: 40rpx;
		color: #FFFFFF;
		font-weight: bold;
	}

	.submit {
		width: 100%;
		min-height: 330rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: space-between;
		background-color: #18253A;
		border-radius: 30rpx;
		border: 4rpx solid #1B315B;
		padding: 40rpx 40rpx 50rpx 40rpx;
		box-sizing: border-box;
		margin-top: 35rpx;
	}

	.qq {
		width: 100%;
		min-height: 400rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		background-color: #18253A;
		border-radius: 30rpx;
		border: 4rpx solid #1B315B;
		padding: 40rpx 40rpx 50rpx 40rpx;
		box-sizing: border-box;
		margin-top: 35rpx;
	}

	.admin {
		width: 100%;
		min-height: 300rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		background-color: #18253A;
		border-radius: 30rpx;
		border: 4rpx solid #1B315B;
		padding: 40rpx 40rpx 50rpx 40rpx;
		box-sizing: border-box;
		margin-top: 35rpx;
	}

	.mine {
		width: 100%;
		height: 600rpx;
		background-color: #18253A;
		border-radius: 30rpx;
		border: 4rpx solid #1B315B;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: space-between;
		padding: 40rpx 40rpx 50rpx 40rpx;
		box-sizing: border-box;
	}

	.scroll-Y {
		width: 100%;
		height: calc(100vh - 270rpx);
		display: flex;
		flex-direction: column;
	}

	.contenr {
		width: 100%;
		height: calc(100vh - 270rpx);
		display: flex;
		flex-direction: column;
		padding: 50rpx 20rpx 10rpx 20rpx;
		box-sizing: border-box;

	}

	.container {
		width: 100%;
		height: 100vh;
		display: flex;
		flex-direction: column;
		background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
	}

	page {
		width: 100%;
		height: 100vh;
		background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
	}
</style>